package com.earl.eatbao.web;

import static org.slf4j.LoggerFactory.getLogger;

import com.earl.eatbao.domain.eatbao.Shopper;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.service.ShopperService;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @created 22-十一月-2016 22:34:46
 */
@RestController
@RequestMapping("/shopper")
public class ShopperController extends BaseController{

	// desc:日记工具logger
	private static final Logger logger = getLogger(ShopperController.class);
	
    @Autowired
    private ShopperService shopperService;

	/**
	 * 查询所有商家信息
	 */
	@ApiOperation(value = "获得所有商家", notes = "获得所有商家", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/all",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findAll(){
    	ResultMessage result = new ResultMessage();
    	result.getResultParm().put("shopperList", shopperService.findAll());
    	result.setServiceResult(true);
    	result.setMessageCode(200L);
    	result.setResultInfo("查询成功");
		return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
	}

	/**
	 * 查询用户是否是商家
	 * @param userId
	 * @return
	 */
    @ApiOperation(value = "查看用户是否为商家", notes = "查看用户是否为商家", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/isshopper/{userId}",method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> isShopper(@PathVariable Long userId){
    	logger.info("<====== 进入isShopper:方法,{[userId] :" + userId + " } ====");
    	// desc: 封装操作结果
    	ResultMessage result = new ResultMessage();
    	result.setServiceResult(shopperService.isShopper(userId));
    	result.setMessageCode(200l);
    	if (result.getServiceResult()) {
    	    result.setResultInfo("请求操作成功");
    	} else {
    	    result.setResultInfo("请求操作失败");
    	}

    	logger.info("==== 退出isShopper方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
    	return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

	/**
	 * 用户认证为商家
	 * @return
	 */
	@ApiImplicitParams({
	            @ApiImplicitParam(name = "userId",value = "用户ID", required = true, dataType = "Long", paramType = "Query"),
	            @ApiImplicitParam(name = "introduction",value = "申请简介", required = true, dataType = "string", paramType = "Query"),
	            @ApiImplicitParam(name = "address",value = "地址", required = true, dataType = "string", paramType = "Query"),
	            @ApiImplicitParam(name = "name",value = "商家真实姓名", required = true, dataType = "string", paramType = "Query"),
	            @ApiImplicitParam(name = "contact",value = "联系方式", required = true, dataType = "Stirng", paramType = "Query")})
	@ApiOperation(value = "用户认证为商家", notes = "用户认证为商家", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/add",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> beShopperApply(
			Shopper shopper,
			@ApiParam(name = "checkCode", required = true, value = "手机验证码")
			@RequestParam(name = "checkCode", required = true)
			String checkCode,
			@ApiParam(value="身份证背景图片") MultipartFile backImg,
			@ApiParam(value="身份证正面图片") MultipartFile fontImg){

		logger.info("<====== 进入beShopperApply:方法,{[businessApply] : } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("shopper", shopperService.beShopperApply(shopper,checkCode, backImg, fontImg));
		if(null != result.getResultParm().get("shopper")) {
			result.setServiceResult(true);
			result.setResultInfo("请求操作成功");
		} else {
			result.setServiceResult(false);
			result.setResultInfo("请求操作失败");
		}
		result.setMessageCode(200l);

		logger.info("==== 退出beShopperApply方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	@ApiOperation(value = "获取商家的状态", notes = "获取商家的姿态", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/getBusinessStatus",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> getBusinessStatus(
	        @ApiParam(name = "userId", required = true, value = "用户ID")
	        @RequestParam(name = "userId", required = true)Long userId) {
		logger.info("<====== 进入getShopperInfo:方法,userId :" + userId + " } ====");
				// desc: 封装操作结果
				ResultMessage result = new ResultMessage();
				Shopper shopper = shopperService.getBusinessStatus(userId);
				result.setMessageCode(200l);
				result.setServiceResult(true);
				if (null != shopper) {
					result.getResultParm().put("status", shopper.getStatus());
				    result.setResultInfo("请求操作成功");
				} else {
					result.getResultParm().put("status", 0);
				    result.setResultInfo("请求操作失败");
				}

				logger.info("==== 退出getBusinessStatus方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
				return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

    /**
     *
     * @param userId
     * @return
     */
	@ApiOperation(value = "获取指点商家", notes = "获取指点商家", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/getShopper/{userId}",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> getShopper(
	        @ApiParam(name = "userId", required = true, value = "用户ID")
            @PathVariable Long userId) {
		logger.info("<====== 进入getShopper:方法,{[userId] :" + userId + " } ====");
				// desc: 封装操作结果
				ResultMessage result = new ResultMessage();
				Shopper shopper = shopperService.getBusinessStatus(userId);
				result.setMessageCode(200l);
				if (null != shopper) {
                    result.setServiceResult(true);
                    result.getResultParm().put("shopper", shopper);
				    result.setResultInfo("请求操作成功");
				} else {
				    result.setServiceResult(false);
					result.getResultParm().put("shopper", null);
				    result.setResultInfo("请求操作失败");
				}

				logger.info("==== 退出ggetShopper方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
				return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
     *
     * @param id
     * @return
     */
	@ApiOperation(value = "获取指点商家", notes = "获取指点商家", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/deleteShopperById/{id}",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> deleteShopperById(
	        @ApiParam(name = "id", required = true, value = "用户ID")
            @PathVariable Long id) {
		logger.info("<====== 进入deleteShopperById:方法,{[id] :" + id + " } ====");
				// desc: 封装操作结果
				ResultMessage result = new ResultMessage();
				result.setMessageCode(200l);
                result.setServiceResult(shopperService.deleteShopperById(id));
				if (result.getServiceResult()) {
				    result.setResultInfo("请求操作成功");
				} else {
				    result.setResultInfo("请求操作失败");
				}

				logger.info("==== 退出deleteShopperById方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
				return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 查询所用待审核的商家
	 * @return
	 */
	@ApiOperation(value = "查询所用待审核的商家", notes = "查询所用待审核的申请", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/waitApply",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findAllWaitApplyShopper() {

		logger.info("<====== 进入findAllWaitApplyShopper:方法,{[] : } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		List<Shopper> shopperList = shopperService.findAllWaitApplyShopper();
		result.getResultParm().put("shopperList",shopperList);
		result.setServiceResult(true);
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出findAllWaitApplyBusiness方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 查找所用通过审核的商家
	 * @return
	 */
	@ApiOperation(value = "查找所用通过审核的商家", notes = "查找所用通过审核的申请", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/passApply",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findAllPassApplyShopper() {
		logger.info("<====== 进入findAllPassApplyShopper:方法,{[] : } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("applyList", shopperService.findAllPassApplyShopper());
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出findAllPassApplyShopper方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 查找所用申请不通过的用户
	 * @return
	 */
	@ApiOperation(value = "查找所用申请不通过的用户", notes = "查找所用申请不通过的申请", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/failApply",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findAllFailApplyShopper() {

		logger.info("<====== 进入findAllFailApplyShopper:方法,{[] : } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("shopperList", shopperService.findAllFailApplyShopper());
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出findAllFailApplyShopper方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 商家申请认证审核失败
	 * @param applyId
	 */
	@ApiOperation(value = "商家申请认证审核失败", notes = "商家申请认证审核失败", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/fail/{applyId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> failOnShopApply(
			@ApiParam(name = "applyId", required = true, value = "申请的ID号")
			@PathVariable Long applyId){
		logger.info("<====== 进入failOnShopApply:方法,{[applyId] : " + applyId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();

		result.setServiceResult(shopperService.failOnShopperApply(applyId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出failOnShopApply方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 商家申请认证审核成功
	 * @return
	 */
	@ApiOperation(value = "商家申请认证审核成功", notes = "商家申请认证审核成功", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/success/{applyId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> successOnShopApply(
			@ApiParam(name = "applyId", required = true, value = "申请的ID")
			@PathVariable Long applyId){

		logger.info("<====== 进入successOnShopApply:方法,{[applyId] :" + applyId + " } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(shopperService.successOnShopperApply(applyId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出successOnShopApply方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
}